package com.jochen.core.screen

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.g2d.TextureAtlas
import com.badlogic.gdx.scenes.scene2d.ui.*
import com.jochen.core.assets.*
import com.jochen.core.audio.AudioManager
import com.jochen.core.screen.transitions.FadeTransition
import com.jochen.core.ui.onChange
import com.jochen.core.ui.onClick
import com.jochen.core.ui.realize.RMenuBackground
import com.jochen.core.ui.realize.UiStyle
import com.jochen.core.world.GdxGame

class SettingsScreen(
    game: GdxGame,
    screenManager: ScreenManager
) : BaseScreen(game,screenManager) {
    
    private val audioManager: AudioManager = game.audioManager
    
    init {
        initUi()
    }
    
    private fun initUi() {
        val assetService = game.assetService 
        val sk = assetService.get(SkinAsset.EARTH)
        
        val isOpenMusicLabel = Label("音乐", sk)
        val isOpenMusicBox = CheckBox("开", sk).apply {
            isChecked = audioManager.IsOpenMusic()
            this.onChange { box ->
                audioManager.setIsOpenMusic(box.isChecked)
            }
        }
        
        val musicVolumeLabel = Label("音量", sk)
        val musicVolume = Slider(0f, 1f, 0.01f, false, sk).apply {
            value = audioManager.getMusicVolume()
            this.onChange { slider ->
                audioManager.setMusicVolume(slider.value)
            }
        }
        
        val soundVolumeLabel = Label("音效", sk)
        val soundVolume = Slider(0f, 1f, 0.01f, false, sk).apply {
            value = audioManager.getSoundVolume()
            this.onChange { slider ->
                audioManager.setSoundVolume(slider.value)
            }
        }
        
        val backMenu = TextButton("返回主菜单", UiStyle.getButtonStyle(game)).apply {
            this.onClick {
                audioManager.playSound(SoundAsset.BUTTON_PRESS)
                setScreen(Menu::class, FadeTransition::class)
            }
        }
        
        val table = Table(sk).apply {
            setFillParent(true)
            defaults().uniform().pad(20f)
            
            add(isOpenMusicLabel)
            add(isOpenMusicBox).row()
            
            add(musicVolumeLabel)
            add(musicVolume).row()
            
            add(soundVolumeLabel)
            add(soundVolume).row()
            
            add(backMenu).colspan(2)
        }
        
        val textureAtlas = game.assetService.get(AtlasAsset.MENU)
        addActor(RMenuBackground(textureAtlas))
        addActor(table)
    }
    
    override fun update(delta: Float) {
        
    }
}